package com.tv189.springbatchtest.contextjobshare;

import com.tv189.springbatchtest.paramcontext.JobConfiguration;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.Map;

/**
 * @ClassName: ExecutionContextJob
 * @Description: 作业中的参数共享
 * @Author: 19202
 * @Date: 2025/5/20 23:04
 * @Version 1.0
 **/
@SpringBootApplication
@EnableBatchProcessing
public class ExecutionContextJob {
    private final JobBuilderFactory jobBuilderFactory;

    private final StepBuilderFactory stepBuilderFactory;

    public ExecutionContextJob(JobBuilderFactory jobBuilderFactory,
                            StepBuilderFactory stepBuilderFactory) {
        this.jobBuilderFactory = jobBuilderFactory;
        this.stepBuilderFactory = stepBuilderFactory;
    }

    /**
     * 如果一个作业的名字是一样的，
     * 执行作业的参数也是一样的，如果这个作业已经被成功的执行过一次了，那么再次被执行的时候就被重复执行了
     *
     * @return {@link Job }
     */
    @Bean
    public Job helloWordJob() {
        return jobBuilderFactory.get("exection-context-job")
                .incrementer(new RunIdIncrementer())
                .start(step1())
                .next(step2())
                .build();
    }

    /**
     * 微
     *
     * @return {@link Tasklet }
     */
    @Bean
    public Tasklet tasklet1() {
        return (stepContribution, chunkContext) -> {
            ExecutionContext sec = chunkContext.getStepContext().getStepExecution().getExecutionContext();
            sec.put("key-step1-step", "value-step1-step");
            System.err.println("-------------------------1-------------------------");
            ExecutionContext jec = stepContribution.getStepExecution().getJobExecution().getExecutionContext();
            jec.put("key-step1-job", "value-step1-job");
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public Tasklet tasklet2() {
        return (stepContribution, chunkContext) -> {
            ExecutionContext sec = chunkContext.getStepContext().getStepExecution().getExecutionContext();
            System.out.println(sec.get("key-step1-step"));
            System.err.println("-------------------------2-------------------------");
            ExecutionContext jec = stepContribution.getStepExecution().getJobExecution().getExecutionContext();
            System.out.println(jec.get("key-step1-job"));
            return RepeatStatus.FINISHED;
        };
    }

    /**
     * step1
     *
     * @return {@link Step }
     */
    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1").tasklet(tasklet1()).build();
    }

    /**
     * step2
     *
     * @return {@link Step }
     */
    @Bean
    public Step step2() {
        return stepBuilderFactory.get("step2").tasklet(tasklet2()).build();
    }

    public static void main(String[] args) {
        SpringApplication.run(ExecutionContextJob.class, args);
    }
}
